//
//  MovieListTableViewController.m
//  doubanMovie
//
//  Created by lanou3g on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MovieListTableViewController.h"
#import "MovieTableViewCell.h"
#import "MovieDetailViewController.h"
#import "DataHandle.h"
#import "MovieDetailView.h"
#import "MovieDetail.h"
#import "WaterFallViewController.h"


@interface MovieListTableViewController ()<dataHandleDelegate>

@end

@implementation MovieListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电影";
    // Uncomment the following line to preserve selection between presentations.
     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] landscapeImagePhone:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movieTableViewCell"];
    __weak typeof(self) weakSelf = self;
    [[DataHandle sharedInstance] getDataSourceWithReloadBlack:^{

        [weakSelf.tableView reloadData];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
    WaterFallViewController *nineVC = [[WaterFallViewController alloc] init];
    [self.navigationController pushViewController:nineVC animated:YES];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return [[DataHandle sharedInstance] rowOfMovie];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieTableViewCell" forIndexPath:indexPath];
    MovieDetail *movieDetail = [[DataHandle sharedInstance] movieDetailForIndexPath:indexPath];
    cell.movieID = movieDetail.movieId;
    cell.movieNameLabel.text = movieDetail.movieName;
    if(nil == movieDetail.imageList && NO == movieDetail.isLoading){
        [movieDetail loadImage];
//        NSLog(@"%@",movieDetail);
        [movieDetail addObserver:self forKeyPath:@"imageList" options:NSKeyValueObservingOptionNew context:(__bridge void *)(indexPath)];
    }else {
        cell.pictureView.image = movieDetail.imageList;
    }
    
    return cell;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@",object);
    UIImage *newImage = change[NSKeyValueChangeNewKey];
    if (newImage == nil) {
        return;
    }else{
        NSIndexPath *indexPath = (__bridge NSIndexPath *)(context);
        NSArray *arr = [self.tableView indexPathsForVisibleRows];
        if ([arr containsObject:indexPath]) {
            MovieTableViewCell *cell = (MovieTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            cell.pictureView.image = newImage;
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
    }
    [object removeObserver:self forKeyPath:@"imageList"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
    MovieDetail *movieDetail = [[DataHandle sharedInstance] movieDetailForIndexPath:indexPath];
    [[DataHandle sharedInstance] movieID:movieDetail.movieId ReloadBlack:^(MovieDetail *model) {
        model.movieName = movieDetail.movieName;
        model.movieId = movieDetail.movieId;
        model.pic_url = movieDetail.pic_url;
        movieDetailVC.movieDetail = model;
        
//        movieDetailVC.title = [NSString stringWithFormat:@"%@",movieDetail.movieName];
        [self.navigationController pushViewController:movieDetailVC animated:NO];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }];
   
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
